﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UWP.Douban.Utils
{
    public class JsonUtil
    {
        internal static T Deserialize<T>(string json) where T : new()
        {
            if (string.IsNullOrEmpty(json)) return default(T);

            //var value = JsonConvert.DeserializeObject(json);
            var model = JsonConvert.DeserializeObject<T>(json);

            return model;
        }

        internal static IEnumerable<T> DeseriEnumerable<T>(string json, string jsonPath)
            where T : new()
        {
            return JObject.Parse(json).SelectTokens(jsonPath)
                .Select(o => Deserialize<T>(o.ToString()));
        }

        internal static dynamic Deserialize(string json)
        {
            if (string.IsNullOrEmpty(json)) return null;
            return JsonConvert.DeserializeObject(json);
        }

        internal static string Serializer(object o)
        {
            return JsonConvert.SerializeObject(o);
        }
    }
}
